import { useMessage } from '@/hooks'
import { defineStore } from 'pinia'
import { collectGame } from '@/api/slots'
import { useAppStore } from '@/store'
import { t } from '@/utils'

const curLanguage = () => {
	const appStore = useAppStore()
	return appStore.lang || 'en'
}

export const useSlotStore = defineStore('slot', {
	state: () => ({
		bigWinsList: [] as any[],
		providerList: [] as any[],
		recommendsList: [] as any[],
		slotsList: [] as any[],
		currentRecomentedTab: 1,
		liveBigDataList: [] as any[],
		gameDetail: {} as any
	}),
	actions: {
		setLiveBigWinnersList(val: any[]) {
			this.liveBigDataList = val
		},
		setProviderList(val: any[]) {
			this.providerList = val
		},
		setRecommendsList(val: any[]) {
			this.recommendsList = val
		},
		setSlotsList(val: any[]) {
			this.slotsList = val
		},
		setGameDetail(val: any) {
			this.gameDetail = val
		},
		updateAllListStatus(val: any) {
			val.forEach((item: any) => {
				this.updateItemStatus(item)
			})
		},
		setLiveWins(val: any) {
			if (val.type === -1) {
				this.bigWinsList = val.data
			} else if (val.type === this.currentRecomentedTab) {
				// this.bigWinsList = val.data[curLanguage()]
				if (this.bigWinsList.length && val.data[curLanguage()].length) {
					for (let i = 0; i < val.data[curLanguage()].length; i++) {
						let flag = false
						for (let j = 0; j < this.bigWinsList.length; j++) {
							if (
								val.data[curLanguage()][i].createTime === this.bigWinsList[j].createTime &&
								val.data[curLanguage()][i].gameId === this.bigWinsList[j].gameId
							) {
								flag = true
								break
							}
						}
						if (flag === false) {
							this.bigWinsList.splice(i, 1, val.data[curLanguage()][i])
							if (this.bigWinsList.length > 20) {
								this.bigWinsList.pop()
							}
						}
					}
				} else {
					this.bigWinsList = val.data[curLanguage()]
				}
			}
		},
		addLiveBigWin(val: any) {
			if (this.liveBigDataList) {
				this.liveBigDataList.unshift(val[curLanguage()])
				setTimeout(() => {
					if (this.liveBigDataList.length && this.liveBigDataList.length > 10) {
						this.liveBigDataList.pop()
					}
				}, 1000)
			}
		},
		async collectGame(parmas: any) {
			try {
				const message = useMessage()
				await collectGame(parmas)
				message.success(t('slotsall.collectSuccess'))
				this.updateCollectedItemStatus({
					isCollect: true,
					id: parmas.id,
					isFavorite: parmas.isFavorite
				})
			} catch (err) {
				console.log(err)
			}
		},
		async cancelCollectGame(parmas: any) {
			const message = useMessage()
			try {
				await collectGame(parmas)
				this.updateCollectedItemStatus({
					isCollect: false,
					id: parmas.id
				})
			} catch (err: any) {
				message.error(err)
			}
		},
		updateItemStatus(val: any) {
			this.recommendsList.forEach((item) => {
				if (item.gameId === val.gameId) {
					item = val
				}
			})
			this.bigWinsList.forEach((item) => {
				if (item.gameId === val.gameId) {
					item = val
				}
			})
			this.slotsList.forEach((item) => {
				if (item.gameId === val.gameId) {
					item = val
				}
			})
			this.liveBigDataList.forEach((item) => {
				if (item.gameId === val.gameId) {
					item = val
				}
			})
			if (this.gameDetail?.gameId === val.gameId) {
				this.gameDetail = val
			}
		},
		updateCollectedItemStatus(val: any) {
			const slotItem = this.slotsList.find((item) => item.id === val.id)
			if (slotItem) {
				slotItem.collectState = val.isCollect
			}
			if (val.isFavorite) {
				this.slotsList.map((item: any, index: number) => {
					if (item.id === val.id) {
						this.slotsList.unshift(this.slotsList.splice(index, 1)[0])
					}
				})
			}
			const recommendItem = this.recommendsList.find((item) => item.id === val.id)
			if (recommendItem) {
				recommendItem.collectState = val.isCollect
			}
			if (val.id === this.gameDetail.id) {
				this.gameDetail.collectState = val.isCollect
			}
		}
	},
	getters: {
		firstLiverBigWinner: (state) => state.liveBigDataList[0]
	}
	// persist: true
})
